package com.dxl.lanju.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.dao.ShopDao;
import com.dxl.service.common.BaseService;
import com.lanju.bean.ShopBean;
import com.lanju.entity.Area;
import com.lanju.entity.Shop;
import com.lanju.entity.User;
import com.lanju.enums.ShopType;

@Service
@Transactional
public class ShopService extends BaseService {
	private static final Logger LOG = Logger.getLogger(ShopService.class);
	@Autowired
	private ShopDao shopDao;
	
	public RespData save(Shop shop) {
		Shop srcShop;
		if (Utils.isNotBlank(shop.getId())) {
			srcShop = this.get(Shop.class, shop.getId());
			srcShop.setName(shop.getName());
			srcShop.setAddress(shop.getAddress());
			srcShop.setAreaId(shop.getAreaId());
			srcShop.setState(shop.getState());
			srcShop.setContact(shop.getContact());
			srcShop.setPath(shop.getPath());
			srcShop.setPathSl(shop.getPathSl());
			srcShop.setTel(shop.getTel());
			srcShop.setType(shop.getType());
		} else {
			srcShop = shop;
		}
		Area area = this.get(Area.class, shop.getAreaId());
		if (Utils.isNotNull(area)) {
			srcShop.setAreaName(area.getName());
		}
		super.save(srcShop);
		return RespData.instance(RespData.SUCCESS, srcShop.getId());
	}
	
	public RespData del(String[] ids) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				this.del(Shop.class, ids);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public RespData state(String[] ids, Integer state) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				Map<String, Object> values = new HashMap<String, Object>();
				values.put("state", state);
				this.update(Shop.class, ids, values);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public Page<ShopBean> list(String keyword, String type, String areaId, Integer state, Integer page, Integer rows) {
		Page<Shop> srcPage = shopDao.list(keyword, type, areaId, state, page, rows);
		Page<ShopBean> rtnPage = convert(srcPage);
		return rtnPage;
	}
	
	public Page<ShopBean> listAudit(String keyword, String type, String areaId, Integer state, Integer status, Integer page, Integer rows) {
		Page<Shop> srcPage = shopDao.listAudit(keyword, type, areaId, state, status, page, rows);
		Page<ShopBean> rtnPage = convert(srcPage);
		return rtnPage;
	}

	private Page<ShopBean> convert(Page<Shop> srcPage) {
		Page<ShopBean> rtnPage =new Page<ShopBean>(srcPage.getPageNo(), srcPage.getPageSize(), srcPage.getTotal());
		List<Shop> srcRows = srcPage.getRows();
		List<ShopBean> rtnRows = new ArrayList<ShopBean>();
		if (Utils.isNotEmpty(srcRows)) {
			ShopBean shopBean;
			for (Shop shop : srcRows) {
				shopBean = new ShopBean();
				rtnRows.add(shopBean);
				
				shopBean.setAddress(shop.getAddress());
				shopBean.setAreaId(shop.getAreaId());
				Area area = get(Area.class, shop.getAreaId());
				shopBean.setAreaName(Utils.isNull(area) ? "" : area.getName());
				shopBean.setContact(shop.getContact());
				shopBean.setId(shop.getId());
				shopBean.setName(shop.getName());
				shopBean.setCreatedTime(shop.getCreatedTime());
				shopBean.setOperator(shop.getOperator());
				shopBean.setPath(shop.getPath());
				shopBean.setPathSl(shop.getPathSl());
				shopBean.setState(shop.getState());
				shopBean.setStatus(shop.getStatus());
				shopBean.setTel(shop.getTel());
				shopBean.setType(shop.getType());
				shopBean.setTypeName(ShopType.getTypeName(shop.getType()));
				shopBean.setUpdatedTime(shop.getUpdatedTime());
				User user = get(User.class, shop.getUserId());
				shopBean.setUsername(Utils.isNull(user) ? "" : user.getName());
				shopBean.setUserId(shop.getUserId());
			}
		}
		rtnPage.setRows(rtnRows);
		return rtnPage;
	}
}
